package com.wzz.controller;


import com.wzz.entity.ImageAttachment;
import com.wzz.mapper.ImageAttachmentMapper;
import com.wzz.vo.CommonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 文件上传
 * @Author xyd
 **/
@Slf4j
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("/picUpload")
public class PicUploadController {


    @Autowired
    private ImageAttachmentMapper imageAttachmentMapper;

    @Value("${picUploadBaseUrl}")
    private  String picUploadBaseUrl;



    @ResponseBody
    @RequestMapping(value = "/image/upload", method = RequestMethod.POST)
    public CommonResult<String> shortOutlineAsr(@RequestParam(name = "file") MultipartFile file) throws IOException {
        String picUrls="";
        String uploadPath = picUploadBaseUrl;
        System.out.println("=====picUploadBaseUrl"+picUploadBaseUrl);
        System.out.println("=====uploadPath"+uploadPath);
        File uploadDirectory = new File(uploadPath);
        if (uploadDirectory.exists()) {
            if (!uploadDirectory.isDirectory()) {
                uploadDirectory.delete();
            }
        } else {
            uploadDirectory.mkdir();
        }
        System.out.println("=====file"+file);
        String fileName = file.getOriginalFilename();
        //判断是否有文件且是否为图片文件
        if(fileName!=null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) {
            String picBaseUrl="/exam-admin-images/" + UUID.randomUUID().toString()+ getFileType(fileName);
            String picUrl=uploadPath +picBaseUrl;
            //创建输出文件对象
            File outFile = new File(picUrl);
            //拷贝文件到输出文件对象
            FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
            ImageAttachment t = new ImageAttachment();
            t.setName(fileName);
            t.setKey("");
            t.setPath(picBaseUrl);
            t.setCreateTime(new Date());
            t.setMediaType(file.getContentType());
            t.setSize(file.getSize());
            imageAttachmentMapper.insert(t);
            picUrls+=picBaseUrl+",";
        }

        System.out.println("=====picUrls"+picUrls);
        picUrls = picUrls.substring(0, picUrls.length() - 1);
        System.out.println("=====picUrls"+picUrls);

       return new CommonResult<>(200, "上传成功", picUrls);
    }




    private Boolean isImageFile(String fileName) {
        String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"};
        if (fileName == null) {
            return false;
        }
        fileName = fileName.toLowerCase();
        for (String type : img_type) {
            if (fileName.endsWith(type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取文件后缀名
     *
     * @param fileName
     * @return
     */
    private String getFileType(String fileName) {
        if (fileName != null && fileName.indexOf(".") >= 0) {
            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
        }
        return "";
    }

}
